using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;

namespace Intuipic
{
	/// <summary>
	/// Interaction logic for DeletionWindow.xaml
	/// </summary>

	public partial class DeletionWindow : System.Windows.Window
	{
		private readonly List<FileInformation> _fileInformations;

		public List<FileInformation> Files
		{
			get
			{
				return _fileInformations;
			}
		}

		public DeletionWindow(IEnumerable<FileInfo> files)
		{
			InitializeComponent();
			_fileInformations = new List<FileInformation>();

			foreach (FileInfo fileInfo in files)
			{
				_fileInformations.Add(new FileInformation(fileInfo));
			}

			DataContext = this;
		}

		private void DeleteFiles(object sender, EventArgs e)
		{
			List<DeletionWindow.FileInformation> failures = new List<DeletionWindow.FileInformation>();

			foreach (DeletionWindow.FileInformation fileInformation in Files)
			{
				if (fileInformation.Delete)
				{
					try
					{
						fileInformation.FileInfo.Delete();
					}
					catch (Exception ex)
					{
						failures.Add(fileInformation);
					}
				}
			}

			if (failures.Count > 0)
			{
				//TODO: show a dialog instead that details the images that could not be deleted
				MessageBox.Show(this, "One or more images could not be deleted.", "Error Deleting", MessageBoxButton.OK, MessageBoxImage.Error);
			}

			DialogResult = true;
		}

		private void DontDeleteFiles(object sender, EventArgs e)
		{
			//just close without deleting
			DialogResult = true;
		}

		public sealed class FileInformation
		{
			private readonly FileInfo _fileInfo;
			private bool _delete;

			public FileInfo FileInfo
			{
				get
				{
					return _fileInfo;
				}
			}

			public bool Delete
			{
				get
				{
					return _delete;
				}
				set
				{
					_delete = value;
				}
			}

			public FileInformation(FileInfo fileInfo)
			{
				_fileInfo = fileInfo;
				_delete = true;
			}
		}
	}
}